﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using UploadClient.Interfaces.Plugins;

namespace UploadClient
{
    public class PluginManager
    {
        public Options Option { get; set; }

        //public IEnumerable<IPlugin> GetPlugins()
        //{
        //    IHost host = new DummyHost();
        //    List<IPlugin> result = new List<IPlugin>();

        //    var region = new RegionShot.RegionShot();
        //    var regionConfig = Option.PConfig.Find(item => item.Guid.Equals(new Guid("{1C0E33DE-27D4-4EE4-941E-DBF424A6D5DC}")));
        //    if (regionConfig != null)
        //        region.Init(host, regionConfig);
        //    else
        //        region.Init(host);
        //    result.Add(region);

        //    var clipboard = new ClipboardMonitoring.ClipboardMonitoring();
        //    var clipConfig = Option.PConfig.Find(item => item.Guid.Equals(new Guid("{6604BFDD-E577-425C-9237-8E4C39626861}")));
        //    if (clipConfig != null)
        //        clipboard.Init(host, clipConfig);
        //    else
        //        clipboard.Init(host);
        //    result.Add(clipboard);

        //    var autostart = new Autostart.Autostart();
        //    var autoConfig = Option.PConfig.Find(item => item.Guid.Equals(new Guid("{3778BC07-0A66-41a6-B422-DC4286EAD9E6}")));
        //    if (autoConfig != null)
        //        autostart.Init(host, autoConfig);
        //    else
        //        autostart.Init(host);
        //    result.Add(autostart);

        //    var context = new Contextmenu.Contextmenue();
        //    var contextConfig = Option.PConfig.Find(item => item.Guid.Equals(new Guid("{0E629942-4942-4195-BC53-A2D8A4125FC7}")));
        //    if (contextConfig != null)
        //        context.Init(host, contextConfig);
        //    else
        //        context.Init(host);
        //    result.Add(context);

        //    return result;
        //}

        /// <summary>
        /// Loads the plugins from folder and initializes them.
        /// </summary>
        /// <param name="host">The IHost.</param>
        /// <returns>A list of IEnumerables.</returns>
        public IEnumerable<IPlugin> GetPlugins()
        {
            IHost host = new DummyHost();
            List<IPlugin> result = new List<IPlugin>();

            foreach (string fileName in Directory.GetFiles("./Plugins"))
            {
                FileInfo fileInfo = new FileInfo(fileName);

                if (fileInfo.Extension.Equals(".dll"))
                {
                    Assembly pluginAssembly = Assembly.LoadFrom(fileName);

                    foreach (Type pluginType in pluginAssembly.GetTypes())
                    {
                        if (!pluginType.IsAbstract)
                        {
                            Type typeInterface = pluginType.GetInterface("IPlugin", true);

                            if (typeInterface != null)
                            {
                                var plugin = (IPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                                plugin.Init(host);

                                var pluginConfig = Option.PcPluginConfig.Find(item => item.Guid.Equals(plugin.GetConfiguration().Select(guid => guid.Guid)));
                                if (pluginConfig != null)
                                    plugin.Init(host, pluginConfig);

                                result.Add(plugin); 
                            }

                            typeInterface = null;
                        }
                    }
                }
            }

            return result;
        }
    }

    public class DummyHost : IHost
    {
        #region IHost Member

        public void AddToUploadlist(string path)
        {

        }

        public void ThrowException(Exception ex)
        {

        }

        public void SetLocalizationRessourceDictionarys(Dictionary<string, Dictionary<string, string>> dictionarys)
        {

        }

        public Window GetWindowHandle()
        {
            return App.Current.MainWindow;
        }

        public string GetTempFileDir()
        {
            return "";
        }

        public string GetProgramName()
        {
            return "";
        }

        public string GetExecutingAssemblyLocation()
        {
            return "";
        }

        public string GetExecutingAssemblyDirectoryName()
        {
            return "";
        }

        public string GetProgramVersion()
        {
            return "";
        }

        public string GetLocalizationRessourceString(string key)
        {
            return key;
        }

        #endregion
    }

}
